package com.ln.property_control.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ln.property_control.mapper.DepartmentMapper;
import com.ln.property_control.mapper.ScrapMapper;
import com.ln.property_control.mapper.ScrapphotoMapper;
import com.ln.property_control.pojo.*;
import com.ln.property_control.service.ScrapService;
import com.ln.property_control.utils.ConstantOssPropertiesUtils;
import com.ln.property_control.utils.GetId;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 报废逻辑处理类
 */
@Service
public class ScrapServiceImpl implements ScrapService {

    @Autowired
    private ScrapMapper scrapMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private ScrapphotoMapper scrapphotoMapper;

    //资产报状态修显示
    @Override
    public Map<String, Object> scrapFindAll(Integer page, Integer limit) {
        Page<ScrapVo> objects = PageHelper.startPage(page, limit);
        //<!--维修申请单展示的信息-->
        List<ScrapVo> scrapList = scrapMapper.selectpScrapPageAll();
        //页数
        long total = objects.getTotal();
        System.out.println(scrapList);
        Map<String, Object> map = new HashMap<>();
        map.put("data", scrapList);
        map.put("count", total);
        map.put("code", 0);
        return map;
    }

    //上传文件到阿里云oss，获取上传的文件(路径)
    @Override
    public String upload(MultipartFile file) {

        //获取到配置文件中设置好 连接oss的数据
        String endpoint = ConstantOssPropertiesUtils.EDNPOINT;
        String accessKeyId = ConstantOssPropertiesUtils.ACCESS_KEY_ID;
        String accessKeySecret = ConstantOssPropertiesUtils.SECRECT;
        String bucketName = ConstantOssPropertiesUtils.BUCKET;

        try {
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            // 上传文件流。
            InputStream inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();
            //生成随机唯一值，使用uuid，添加到文件名称里面
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            fileName = uuid + fileName;
            //按照当前日期，创建文件夹，上传到创建文件夹里面
            //  2021/02/02/01.jpg
            String timeUrl = new DateTime().toString("yyyy/MM/dd");
            fileName = timeUrl + "/" + fileName;
            //调用方法实现上传
            ossClient.putObject(bucketName, fileName, inputStream);
            // 关闭OSSClient。
            ossClient.shutdown();
            //上传之后文件路径
            // https://yygh-atguigu.oss-cn-beijing.aliyuncs.com/01.jpg
            String url = "https://" + bucketName + "." + endpoint + "/" + fileName;
            //返回
            return url;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    //所在组织下拉框
    @Override
    public List<Department> organisation(Integer id) {
        List<Department> departmentList = departmentMapper.organisation(id);
        return departmentList;

    }

    //提出申请维修
    @Override
    public Map<String, Object> insertScrap(Scrap scrap, int[] faultId, Scrapphoto scrapphoto) {
        Map<String, Object> map = new HashMap<>();
        //修改 获取资产的状态
        Integer propertystatus = scrapMapper.selectPropertystatus(scrap.getPropertyId());
        scrap.setPropertystatus(propertystatus);
        System.out.println("这是获取到的资产状态号" + propertystatus);
        //获取报修单号
        String b = "BX";
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String format1 = format.format(date);
        scrap.setScrapId(b + scrap.getPropertyId());
        scrap.setScrapState(6);
        scrap.setStartScrapTime(new Date());
        scrap.setApplyState(1);
        System.out.println();
        int insert = scrapMapper.insert(scrap);
        if (insert < 1) {
            map.put("msg", "维修申请失败");
            return map;
        }
        //修改资产表里的状态
        Integer property_status = 4;
        String property_id = scrap.getPropertyId();
        scrapMapper.updatePropertystatus(property_status, property_id);

        scrapphoto.setScrap_id(scrap.getScrapId());
        int insert1 = scrapphotoMapper.insert(scrapphoto);
        if (insert < 1) {
            map.put("msg", "上传图片失败");
            return map;
        }
        //添加故障类型
        scrapMapper.insertfault(scrap.getScrapId(), faultId);
        //添加到审核流程历史表
        Maintainhistory maintainhistory = new Maintainhistory();
        maintainhistory.setScrap_id(scrap.getScrapId());
        maintainhistory.setApply_state(0);
        maintainhistory.setResponsible(scrap.getResponsible());
        maintainhistory.setScrap_operator(scrap.getScrapOperator());
        maintainhistory.setAudittime(scrap.getStartScrapTime());
        int i = scrapMapper.insertHistory(maintainhistory);

        map.put("msg", "提出维修申请成功");
        return map;
    }

    //获取维修申请单的详情（）
    @Override
    public ScrapVo selectScrapAll(String scrapId) {
        ScrapVo scrap1 = scrapMapper.selectOned(scrapId);

        return scrap1;
    }

    @Override
    public Scrapphoto selectPhotoUrl(String scrapId) {
        return scrapphotoMapper.selectOned(scrapId);
    }


    //通过申请
    @Override
    public int affirm(ScrapVo scrapVo) {
        return scrapMapper.updateapply_state(scrapVo);
    }

    //查询资产是否存在
    @Override
    public Map<String, Object> selectPropertyExist(String property_id) {
        Map<String, Object> map = new HashMap<>();
        int i = scrapMapper.selectPropertyExist(property_id);
        if (i < 1) {
            map.put("coud", 501);
            map.put("msg", "资产不存在或资产编号输入有误");
            return map;
        }
        map.put("coud", 200);
        map.put("msg", "资产存在");
        return map;
    }

    //申请单的历史
    @Override
    public Map<String, Object> SelectHistory(String property_id) {
        List<ScrapVo> scrapVoList = scrapMapper.SelectHistory(property_id);
        Map<String, Object> map = new HashMap<>();
        map.put("data", scrapVoList);
        map.put("code", 0);
        return map;
    }

    //将操作添加到日志中
    @Override
    public void insertHistory(Maintainhistory maintainhistory) {
        scrapMapper.insertHistoryCourse(maintainhistory);
    }

    //修改资产状态
    @Override
    public Map<String, Object> updateScrapState(Integer scrap_state, String scrap_id, String property_id) {
        Map<String, Object> map = new HashMap<>();

        int i = scrapMapper.updateScrapState(scrap_state, scrap_id);

        map.put("code", i);
        return map;

    }

    //查询所有的故障类型
    @Override
    public List<ScrapVo> selectFaulttype() {
        List<ScrapVo> FaulttypeList = scrapMapper.selectFaulttype();
        return FaulttypeList;
    }

    //查询中间表里的类型ia
    @Override
    public List<ScrapVo> selectLIstScfa(String scrap_id) {
        List<ScrapVo> scrapVoList = scrapMapper.selectLIstScfa(scrap_id);
        return scrapVoList;
    }

    //记录档案
    @Override
    public String insertRecord(Record record) {
        int i = scrapMapper.insertRecord(record);
        if (i > 0) {
            return "文档存入成功";
        } else {
            return "文档存入失败";
        }
    }


}
